package com.yidiandao.business.ui.main.shoppingcart.choosecoupon;

import com.yidiandao.business.data.bean.Coupon;
import com.yidiandao.business.data.model.ICouponModel;
import com.yidiandao.business.ui.widget.listview.DisplayListItem;
import com.yidiandao.business.ui.widget.listview.displaylistitem.ChooseCouponListItem;
import com.yidiandao.business.ui.widget.view.SuperLoadingLayout;

import java.util.List;

/**
 * 选择优惠券-Presenter
 */
public class ChooseCouponPresenter implements ChooseCouponContract.Presenter {

    private ChooseCouponContract.View mChooseCouponView;

    /**
     * 可选择的红包列表
     */
    private List<Coupon> mCoupons;

    private Coupon mChooseCoupon;

    private boolean mUseCoupon;

    private ICouponModel mCouponModel;

    public ChooseCouponPresenter(ChooseCouponContract.View view, ICouponModel couponModel) {
        mChooseCouponView = view;
        mCouponModel = couponModel;

        mChooseCouponView.setPresenter(this);
    }

    @Override
    public void start() {
        mCouponModel.getCartCouponList(mChooseCouponView.getPagerTag(), new ICouponModel.OnCartUsefulCouponListener() {
            @Override
            public void onSuccess(List<Coupon> source, List<DisplayListItem> displayListItems, Coupon selected) {

                // 保存选中的红包
                // 注意:可能为null --> 未选择红包
                mChooseCoupon = selected;
                mUseCoupon = (selected != null);
                // 状态回显
                mChooseCouponView.refreshUseCouponState(mUseCoupon);
                mChooseCouponView.setListViewVisibility(mUseCoupon);

                mCoupons = source;
                mChooseCouponView.refreshContent(mChooseCouponView.checkResultObject(source));
                mChooseCouponView.setDisplayListItems(displayListItems);
            }

            @Override
            public void onFailed() {
                mChooseCouponView.refreshContent(SuperLoadingLayout.STATE_ERROR);
            }
        });
    }

    @Override
    public void onItemClick(ChooseCouponListItem listItem) {

        // 使用红包
        mUseCoupon = true;

        // 设置选中当前的红包
        mChooseCoupon = listItem.getContentData();
        mChooseCoupon.setIsSelected(1);

        for (Coupon other : mCoupons) {
            if (other != mChooseCoupon) {
                other.setIsSelected(0);
            }
        }

        mChooseCouponView.notifyDataSetChanged();
    }

    @Override
    public void onConfirmChoose() {

        // 选择红包
        String couponId;
        if (mUseCoupon) {
            couponId = mChooseCoupon == null ? "0" : mChooseCoupon.getPaperId();
        } else {
            couponId = "0";
        }

        mCouponModel.chooseCoupon(mChooseCouponView.getPagerTag(), couponId, new ICouponModel.OnAccessNetListener() {
            @Override
            public void onSuccess(List<DisplayListItem> displayListItems, boolean emptyResult, boolean hasMore, String totalItem) {
                mChooseCouponView.finishActivity();
            }

            @Override
            public void onFailed() {

            }
        });
    }

    @Override
    public void setUseCoupon(boolean useCoupon) {
        mUseCoupon = useCoupon;
    }
}
